package be.gnx.fogo.application.ui;

import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.hibernate.Transaction;

import be.gnx.fogo.application.dao.LabelDao;
import be.gnx.fogo.application.dao.PersonDao;
import be.gnx.fogo.application.event.SearchBarModifiedEvent;
import be.gnx.fogo.application.event.SearchBarModifiedListener;
import be.gnx.fogo.application.model.Category;
import be.gnx.fogo.application.model.Country;
import be.gnx.fogo.application.model.Language;
import be.gnx.fogo.application.model.Sexe;
import be.gnx.fogo.application.ui.MenuBar;
import be.gnx.fogo.application.util.HibernateUtil;
import be.gnx.fogo.application.util.LabelResourceBundle;

public class Fogo extends Composite {

  private Vector<SearchBarModifiedListener> searchBarModifiedListeners = new Vector<SearchBarModifiedListener>();

  private MenuBar menuBar;

  private SearchBar searchBar;

  private FamilyCard familyCard;

  public Fogo(Composite composite, int style, ResourceBundle resourceBundle) {
    super(composite, style);
    menuBar = new MenuBar(this.getShell(), SWT.NONE, resourceBundle);
    searchBar = new SearchBar(this, SWT.NONE, resourceBundle);
    familyCard = new FamilyCard(this, SWT.NONE, resourceBundle);
    resize();

    menuBar.setToolTipText("");

    searchBar.addSearchBarModifiedListener(new SearchBarModifiedListener() {
      public void handleEvent(SearchBarModifiedEvent event) {
        PersonDao personDao = new PersonDao();
        SearchBar searchBar = (SearchBar) event.widget;
        searchBar.removeSearchBarModifiedListener(this);

        Combo combo = event.getSearchCombo();
        boolean asFamily = event.isAsFamily();
        Transaction transaction = HibernateUtil.beginTransaction();
        try {
          Point selection = combo.getSelection();
          String searchString = combo.getText();
          Hashtable<Long, String> names = personDao.findByNames(searchString);
          combo.setItems(names.values().toArray(new String[0]));
          combo.setText(searchString);
          combo.setSelection(selection);
          if (names.size() == 1) {
            familyCard.setPerson(personDao.get(names.keys().nextElement()), asFamily);
            combo.setText(names.values().iterator().next());
          }
        } finally {
          transaction.commit();
          searchBar.addSearchBarModifiedListener(this);
        }
      }
    });

    addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent disposeEvent) {
        Fogo.this.widgetDisposed(disposeEvent);
      }
    });

    addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent controlEvent) {
        Fogo.this.controlResized(controlEvent);
      }
    });

  }

  public void addSearchBarModifiedListener (SearchBarModifiedListener searchBarModifiedListener) {
    checkWidget();
    if (searchBarModifiedListener == null) {
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    } else {
      searchBarModifiedListeners.add(searchBarModifiedListener);
    }
  }

  public void removeSearchBarModifiedListener (SearchBarModifiedListener searchBarModifiedListener) {
    checkWidget();
    if (searchBarModifiedListener == null) {
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    } else {
      searchBarModifiedListeners.remove(searchBarModifiedListener);
    }
  }

  private void widgetDisposed(DisposeEvent disposeEvent) {
    // TODO;
  }

  private void controlResized(ControlEvent controlEvent) {
    resize();
  }

  private void resize() {
    Point searchBarSize = searchBar.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    searchBar.setBounds(0, 0, searchBarSize.x, searchBarSize.y);
    familyCard.setBounds(0, searchBarSize.y, getParent().getClientArea().width, getParent().getClientArea().height - searchBarSize.y);
  }

  public static void main(String[] args) {
    LabelDao labelDao = new LabelDao();

    Country.initialise();
    Language.initialise();
    Sexe.initialise();
    Category.initialise();

    Transaction transaction = HibernateUtil.beginTransaction();
    labelDao.initialise();
    transaction.commit();

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(600, 600);
    shell.setText("Fogo Test");

    ResourceBundle labelResourceBundle =
      ResourceBundle.getBundle("be.gnx.fogo.application.util.LabelResourceBundle", Locale.ENGLISH);
    ((LabelResourceBundle) labelResourceBundle).setLocale(Locale.ENGLISH);
//    System.err.println("UUGGHH reosure = " + labelResourceBundle.getString("test"));

    final Fogo fogo = new Fogo(shell, SWT.NONE, labelResourceBundle);
    fogo.setSize(shell.getClientArea().width, shell.getClientArea().height);

    shell.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent controlEvent) {
        fogo.setSize(shell.getSize());
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

}
